﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 用于UDP发送的网络服务类
        /// </summary>
        static UdpClient udpcRecv = null;

        static IPEndPoint localIpep = null;

        private void button1_Click(object sender, EventArgs e)
        {
            byte[] bytRecv = udpcRecv.Receive(ref localIpep);
            string message = Encoding.Default.GetString(bytRecv, 0, bytRecv.Length);
            textBox1.Text = string.Format("{0}[{1}]", localIpep, message);

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            localIpep = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 8899); // 本机IP和监听端口号
            udpcRecv = new UdpClient(localIpep);
        }
    }
}
